cc.Class({
    extends: cc.Component,

    properties: {
        effectNode: cc.Node,
        listNode: cc.Node,
        showNode: cc.Node,
        bgSprite: cc.Sprite,
    },


    onLoad () {
        cc.systemEvent.on(cc.SystemEvent.EventType.KEY_UP, (event) => {
            if (event.keyCode === cc.macro.KEY.Delete
                || event.keyCode === cc.macro.KEY.backspace) {
                this.onBtnClickBackList();
            }
        });

        this._posMap = {};
        let pos = cc.sys.localStorage.getItem('pos');
        if (pos) {
            this._posMap = JSON.parse(pos);
        }

        cc.debug.setDisplayStats(false);
        window.TestScene = this;


        let input = document.createElement('input');
        this.input = input;
        input.type = 'file';
        input.style.display = 'none';
        input.onchange = (event, data) => {
            let file = input.files.item(0);
            let type = null;
            if (file.type === 'image/png') {
                type = 'png';
            } else if (file.type === 'image/jpeg') {
                type = 'jpeg';
            } else {
                console.log(`不支持的图片类型:${file.type}`);
            }
            if (type) {
                let url = window.URL.createObjectURL(file);
                cc.loader.load({ url: url, type: type }, (error, texture) => {
                    this.bgSprite.spriteFrame = null;
                    if (error) {
                        console.error(`加载用户图片失败: ${url}`);
                        return;
                    }
                    let bgNode = this.bgSprite.node;
                    bgNode.width = texture.width;
                    bgNode.height = texture.height;
                    bgNode.x = bgNode.y = 0;

                    this.bgSprite.spriteFrame = new cc.SpriteFrame(texture);
                });
            }
        };
        document.body.appendChild(input);
    },

    start () {
        this.onBtnClickBackList();
    },
    onBtnClickChangeBg () {
        this.input.click();
    },
    onBtnClickBackList () {
        this.cur = null;
        this.listNode.active = true;
        this.showNode.active = false;
        this.effectNode.destroyAllChildren();
    },
    showEffect (prefab) {


        this.cur = null;
        this.listNode.active = false;
        this.showNode.active = true;
        this.effectNode.destroyAllChildren();


        let rootNode = new cc.Node();
        this.effectNode.addChild(rootNode);

        let prefabUUID = prefab._uuid;
        rootNode.prefabUUID = prefabUUID;
        let pos = this._posMap[prefabUUID];
        if (pos) {
            rootNode.setPosition(cc.v2(pos.x, pos.y));
            // rootNode.setPosition(cc.v2(0, 0));
        }

        let node = cc.instantiate(prefab);
        node.setPosition(0, 0);
        rootNode.addChild(node);
        let animation = node.getComponent(cc.Animation);
        if (animation) {
            let clips = animation.getClips();
            console.log(`动画数量:${clips.length}`);
            if (clips.length > 0) {
                let clip = clips[0];
                let name = clip.name;
                let state = animation.play(name);

                this.cur = {
                    state: state,
                    animation: animation,
                    name: name,
                    node: rootNode,
                };

            }
        }
    },
    onBtnClickRePlay () {
        if (this.cur) {
            if (!this.cur.state.isPlaying) {
                this.cur.animation.play(this.cur.name);
            }
        }
    },
    onBtnClickToOrigin () {
        if (this.cur) {
            this.cur.node.setPosition(0, 0);
        }
    },

    // update (dt) {},
});
